package com.xywy.common.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.xywy.R;

/**
 * 自动变大的
 * 
 * @author yangyu
 */
public class AnimImageView extends LinearLayout {

	Context mContext;

	public AnimImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public AnimImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public AnimImageView(Context context) {
		super(context);
		init(context);
	}

	AnimationSet animationSet;
	Handler handler;

	ImageView imageView1;
	ImageView imageView2;

	public void init(Context context) {
		mContext = context;
		animationSet = new AnimationSet(true);
		start();
	}

	public void start() {
		View view = View.inflate(mContext, R.layout.wedget_huanxing, this);

		imageView1 = (ImageView) view.findViewById(R.id.iv_yuanhuan1);
//		imageView2 = (ImageView) view.findViewById(R.id.iv_yuanhuan2);
		handler = new Handler();
		begainAnim(imageView1, 0, 500);
		begainAnim(imageView1, 0, 1000);
	}

	private void begainAnim(final ImageView imageView, long time,
			final long offset) {
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				ScaleAnimation sa = new ScaleAnimation(0.0f, 1.7f, 0.0f, 1.7f,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
				aa.setRepeatCount(999);
				sa.setRepeatCount(999);
				animationSet.addAnimation(sa);
				animationSet.addAnimation(aa);
				animationSet.setDuration(2000);
				animationSet.setRepeatCount(999);
				animationSet.setStartOffset(offset);
				animationSet.setFillAfter(true);// 动画执行完后是否停留在执行完的状态
				imageView.startAnimation(animationSet);

			}
		}, time);
	}
}
